package com.example.springsimple.controller;

import com.example.springsimple.pojo.dto.ResponseMessage;
import com.example.springsimple.pojo.dto.UserDto;
import com.example.springsimple.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.example.springsimple.pojo.User;

@RestController  //返回对象 转换成json
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    //添加
    @PostMapping
    public ResponseMessage<User> add(@Validated @RequestBody UserDto user) {
        User userNew = userService.add(user);
        return ResponseMessage.success(userNew);
    }
    @GetMapping("/{userId}")
    public ResponseMessage add(@PathVariable Integer userId){
        User userNew= userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }
    @PutMapping("/")
    public ResponseMessage edit(@Validated @RequestBody UserDto user ){
        User userNew =userService.edit(user);
        return ResponseMessage.success(userNew);
    }
    @DeleteMapping("/{userId}")
    public ResponseMessage delete (@PathVariable Integer userId){
       userService.delete(userId);
        return ResponseMessage.success();
    }
}
